package com.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.http.MyHttpClient;
import com.phone.html5.Html5Action;
import com.xt.base.wireless.server.http.Encryption;
import com.xt.base.wireless.util.ByteUtil;

/**
 * Author:杨洪波
 * Date:2014-3-29
 *
 */
public class Yunwei extends Html5Action{
	private static final long serialVersionUID = -1273407664359478108L;
	private MyHttpClient myHttpClient;
	private String sendMsg;
	private String agent;
	private List<Object> list;
	private String agentName;
	private boolean notDealResult=false;
	private boolean agentIsHex=true;
	private String notDealResultStr="&notDealResult=true";
	private Map<String,String> map;
	public String execute(){
		if(agentIsHex){//jsp页面访问时，跳过这一步
			agent = Encryption.d(new String(ByteUtil.hexStringToBinary(agent)));//Encryption.d2(agent);
		}
		notDealResult = agent.indexOf(notDealResultStr)>=0?true:false;
		req();
		if(!notDealResult){
			list = parseData(sendMsg);
			JSONObject jsonObject = JSONObject.fromObject( list);
			sendMsg = jsonObject.toString();
		}
		return "j2mepage";
	}
	public String toHtml(){
		if(agentIsHex){//jsp页面访问时，跳过这一步
			agent = Encryption.d(new String(ByteUtil.hexStringToBinary(agent)));//Encryption.d2(agent);
		}
		req();
		if(!notDealResult){
			list = parseData(sendMsg);
		}
		dealAgentName();
		this.resultString=(String)map.get(agentName);
		return "resultString";
	}

	private void req(){
		agent=agent.replace(notDealResultStr,"");
		String[] a = agent.split("\\?");
		sendMsg = myHttpClient.request(agent,"");//a[0]+"?OpenAgent", a[1]);
	}
	public static List<Object> parseData(String s){
		List<Object> list = new ArrayList<Object>();
		String split;
		if(StringUtils.isNotEmpty(s)){
			String[] a;
			split=s.indexOf("@")>=0?"@":(s.indexOf("#")>=0?"#":(s.indexOf("\\|")>=0?"\\|":null));
			if(split==null){
				return null;
			}
			a = s.split(split);
			List<Object> list2;
			for(String s2 : a){
				list2 = parseData(s2);
				list.add(list2==null?s2:list2);
			}
		}
		return list;
	}
	
	private void dealAgentName(){
		this.agentName=this.agent.substring(this.agent.indexOf("(")+1
				,this.agent.indexOf(")"));
	}
	
	public MyHttpClient getMyHttpClient() {
		return myHttpClient;
	}
	public void setMyHttpClient(MyHttpClient myHttpClient) {
		this.myHttpClient = myHttpClient;
	}
	public String getSendMsg() {
		return sendMsg;
	}
	public void setSendMsg(String sendMsg) {
		this.sendMsg = sendMsg;
	}
	public String getAgent() {
		return agent;
	}
	public void setAgent(String agent) {
		this.agent = agent;
	}
	public List<Object> getList() {
		return list;
	}
	public void setList(List<Object> list) {
		this.list = list;
	}
	public boolean isAgentIsHex() {
		return agentIsHex;
	}
	public void setAgentIsHex(boolean agentIsHex) {
		this.agentIsHex = agentIsHex;
	}
	public String getAgentName() {
		return agentName;
	}
	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}
	public boolean isNotDealResult() {
		return notDealResult;
	}
	public void setNotDealResult(boolean notDealResult) {
		this.notDealResult = notDealResult;
	}
	public String getNotDealResultStr() {
		return notDealResultStr;
	}
	public void setNotDealResultStr(String notDealResultStr) {
		this.notDealResultStr = notDealResultStr;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
}
